package com.yh.csx.bsf.flow.core.base;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.yh.csx.bsf.core.util.JsonUtils;
import com.yh.csx.bsf.core.util.StringUtils;
import com.yh.csx.bsf.flow.core.util.XmlUtil;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: chejiangyi
 * @version: 2019-11-03 11:34
 * 模型xml定义
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("ModelXml")
public  class Xml{
    @XStreamAlias("Variables")
    private List<Variable> variables;
    @XStreamAlias("Nodes")
    private List<Model.Node> nodes;
    @XStreamAlias("FormPermissions")
    private List<Model.FormPermission> formPermissions;

    public String toJson(){
        return JsonUtils.serialize(this);
    }

    public Xml fromJson(String json){
        return JsonUtils.deserialize(json,Xml.class);
    }

    public String toXml(){
        return  XmlUtil.serialize(this);
    }

    public Xml fromXml(String xml){
        return XmlUtil.deserialize(xml,Xml.class);
    }
    public void checkXml(){
        if(variables==null){
            throw new FlowException("流程变量未配置");
        }
        for(val variable:variables){
            if(StringUtils.isEmpty(variable.getKey())){
                throw new FlowException("流程变量key不能为空");
            }
            variable.setDesc(StringUtils.nullToEmpty(variable.getDesc()));
            variable.setValue(StringUtils.nullToEmpty(variable.getValue()));
        }
        if(nodes==null)
            throw new FlowException("流程节点未配置");
        for(val node:nodes){
            if(StringUtils.isEmpty(node.getTitle()))
                throw new FlowException("流程节点标题未配置");
            var count =  nodes.stream().filter(c->node.getTitle().equals(c.getTitle())).count();
            if(count>1){
                throw new FlowException("流程节点"+node.getTitle()+"标题重复,请保证唯一");
            }
            node.setBeginScript(StringUtils.nullToEmpty(node.getBeginScript()));
            node.setEndScript(StringUtils.nullToEmpty(node.getEndScript()));
            if(node.getRoutes()==null){
                node.setRoutes(new ArrayList<Model.Route>());
            }
            for(var route:node.getRoutes()){
                if(StringUtils.isEmpty(route.getNext())){
                    throw new FlowException("路由未指向节点");
                }
                var find=nodes.stream().filter(c->c.getTitle().equals(route.getNext())).findFirst();
                if(!find.isPresent()){
                    throw new FlowException("路由指向节点"+route.getNext()+"不存在");
                }
                route.setTitle(StringUtils.nullToEmpty(route.getTitle()));
            }
            if(node.getTypeEnum()== Model.TypeEnum.Man) {
                if(node.getApproveResults()==null||node.getApproveResults().size()==0){
                    throw new FlowException("节点"+node.getTitle()+"未配置审批结果");
                }
                for(val r:node.getApproveResults()){
                    if(StringUtils.isEmpty(r))
                        throw new FlowException("节点"+node.getTitle()+"审批结果不能为空");
                }

                if(node.getApproveUser()==null){
                    throw new FlowException("节点"+node.getTitle()+"未配置审批人");
                }
                if(node.getApproveUser().getApproveNum()==0){
                    throw new FlowException("节点"+node.getTitle()+"审批人数不能为0");
                }
                if(node.getApproveUser().getChooseUsers()==null||node.getApproveUser().getChooseUsers().size()==0)
                    throw new FlowException("节点"+node.getTitle()+"未配置选择审批人");
                for(val r:node.getApproveUser().getChooseUsers()){
                    if(StringUtils.isEmpty(r.getValue()))
                        throw new FlowException("节点"+node.getTitle()+"选择审批人取值不能为空");
                }
            }
        }
    }
}